package com.mrtan.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.mrtan.bean.Account;
import com.mrtan.bean.Permission;
import com.mrtan.bean.Role;
import com.mrtan.service.AccountService;
import com.mrtan.vo.LoginVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Validated
@RestController
public class Logincontroller {
    @Resource
    private AccountService accountService;
    @GetMapping("/logout")
    public SaResult logout(HttpServletRequest request){
        String token = request.getHeader("hm-token");
        System.out.println(token);
        // 检查token是否为null或者是否包含无效的格式
        if (token == null || "{{hm-token}}".equals(token)) {
            return SaResult.error("用户未登录！").setCode(400);
        }
        // 进一步检查token的有效性，例如是否为空白字符串
        if (token.trim().isEmpty()) {
            return SaResult.error("非法的令牌，请重新登录！").setCode(400);
        }
        StpUtil.logout();
        return SaResult.ok("注销成功");
    }

    /*@GetMapping
    public SaResult login(@NotBlank String username, String password){
        return SaResult.ok();
    }*/

    @PostMapping("/login")
    public SaResult login(@RequestBody @Validated LoginVO loginVO){
        Account account = accountService.findByUsernameAndPassword(loginVO.getUsername(), loginVO.getPassword());
        /*if (account == null){
            return SaResult.error("登录失败:用户名或密码错误");
        }*/
        StpUtil.login(account.getId());
        /*
            前端需要什么
            1.用户名
            2.用户的角色列表
            3.用户的权限列表
         */
        List<String> roleList = getRoleList(account);
        List<String> permissionList = getPermissionList(account);
        account.setRoles(null); //减少数据传输量，从而提高性能
        return SaResult.ok()
                .set("token",StpUtil.getTokenValue())
                .set("roles", roleList)
                .set("permissions", permissionList)
                .set("account",account);
    }

    public List<String> getPermissionList(Account account) {
        //得到当前登录用户的权限列表
        Set<Role> roles = account.getRoles();
        Set<String> permissions = new HashSet<>();
        if (roles!=null){
            for (Role role : roles) {
                Set<Permission> permissions1 = role.getPermissions();
                if (permissions1!=null){
                    for (Permission permission : permissions1) {
                        permissions.add(permission.getCode());
                    }
                }
            }
        }
        return new ArrayList<>(permissions);
    }

    public List<String> getRoleList(Account account) {
        //得到当前登录用户的角色列表
        Set<Role> roles = account.getRoles();
        Set<String> roleCodes = new HashSet<>();
        if (roles !=null){
            for (Role role : roles) {
                roleCodes.add(role.getCode());
            }
        }
        return new ArrayList<>(roleCodes);
    }
}
